package guoanmaker.operator.business.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import guoanmaker.operator.business.model.OperatorHelp;
import guoanmaker.operator.business.modelnobind.OperatorHelpMove;
import guoanmaker.operator.business.modelnobind.ProvideRoomMove;
import guoanmaker.operator.business.repository.OperatorHelpRepository;

@Service
public class OperatorHelpService implements OperatorHelpServiceInterface {
	@Autowired
	private OperatorHelpRepository operatorHelpRepository;
	
	@PersistenceContext
	private EntityManager manager;
	
	@Override
	public OperatorHelp saveOperatorHelp(OperatorHelp operatorHelp) {
		String title=operatorHelp.getTitle();
		String jpql="select r from OperatorHelp r where r.title=:title";
		Query query=manager.createQuery(jpql).setParameter("title", title);
		List<OperatorHelp> list=query.getResultList();
		if(list!=null&&operatorHelp.getId()==null&&list.size()>0){
			return null;
		}
		if(list!=null&&operatorHelp.getId()!=null&&list.size()>1){
			return null;
		}
		return operatorHelpRepository.save(operatorHelp);
	}
	
	  @Override
	    public Map<String,Object> getAllHelpByPage(Integer page, Integer size) {
		  Map<String,Object> map=new HashMap<String,Object>();
			String jpql="select new guoanmaker.operator.business.modelnobind.OperatorHelpMove(o.id,o.title,o.questionTitle,"
					+ "o.questionContent,o.status,o.createDate,u.id,u.username) "
					+ "from OperatorHelp o left join o.operatorUser u order by o.createDate desc";
			Query query = manager.createQuery(jpql);
			int count=query.getResultList().size();
			List<OperatorHelpMove> list=query.setMaxResults(size)
					.setFirstResult(page*size)
					.getResultList();
			map.put("count", count);
			map.put("list", list);
			return map;
	        		
	        		
	    }
	  public List<OperatorHelpMove> getAllHelp() {
		  String jpql="select new guoanmaker.operator.business.modelnobind.OperatorHelpMove(o.id,o.title,o.questionTitle,"
				  + "o.questionContent,o.status,o.createDate,u.id,u.username) "
				  + "from OperatorHelp o left join o.operatorUser u where o.status=1 order by o.createDate desc";
		  Query query = manager.createQuery(jpql);
		  List<OperatorHelpMove> list=query.getResultList();
		  return list;
	  }
	  
	  @Override
	    public Page<OperatorHelp> getAllHelpBystatusPage(Integer page, Integer size) {
		  Pageable pageable = new PageRequest(page, size);
		  return operatorHelpRepository.findByStatus(pageable);
	        		
	        		
	    }
	  
	  @Override
	  @Transactional
	    public void deleteHelpById(String id) {
		  operatorHelpRepository.delete(id);
	    }
	  
	  @Override
	    public OperatorHelp getHelpById(String id) {
	        return operatorHelpRepository.findOne(id);
	    }


}
